package com.example.demoJDBC.txt;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.example.demoJDBC.txt.DemoData;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * description :
 * author :song
 * create :2022-04-13 14:48:00
 */
public class Txttext {
    public static void main(String[] args) {
        List<DemoData> demoDataList = new ArrayList<>();
        // 写法1：JDK8+ ,不用额外写一个DemoDataListener
        // since: 3.0.0-beta1
        String fileName = "F:\\zdDocument\\project\\design\\demoJdbc\\用户信息收集表.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> {
            for (DemoData demoData : dataList) {
                demoData.getCard();
                demoDataList.add(demoData);
                System.out.println("demoData:" + demoData.toString());
            }
        })).sheet().doRead();


        FileWriter writer = null;
        try {
            writer = new FileWriter("用户信息更新脚本.txt");
            writer.write("用户信息总数：" + demoDataList.size() + "\r\n");;
            for (DemoData entity : demoDataList) {
                writer.write("update t_users_info set CARD_NUMBER = '" + entity.getCard() + "' where phid = (select USER_ID from t_users_local_oauth where account = '" + entity.getCode() + "');" + "\r\n");
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("e:" + e.getMessage());
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
